﻿/** EasyWeb spa v3.1.6 date:2020-03-11 License By http://easyweb.vip */

layui.define(function (exports) {
    var setter = {
        version: new Date().getTime(),   
        baseServer: 'json/', // 接口地址，实际项目请换成http形式的地址
        tableName: 'think',  // 存储表名
        pageTabs: true,   // 是否开启多标签
        openTabCtxMenu: false,   // 是否开启Tab右键菜单
        maxTabNum: 20,  // 最多打开多少个tab
        viewPath: 'view', // 视图位置
        viewSuffix: '.html',  // 视图后缀
        defaultTheme: 'theme-admin',  // 默认主题
        reqPutToPost: false,  // req请求put方法变成post
        cacheTab: true,  // 是否记忆Tab
        apiNoCache: true,  // ajax请求json数据不带版本号

        // 获取缓存的token
        getToken: function () {
            var cacheData = layui.data(setter.tableName);
            if (cacheData) {
                return cacheData.token;
            }
        },
        // 清除token
        removeToken: function () {
            layui.data(setter.tableName, {
                key: 'token',
                remove: true
            });
        },
        // 缓存token
        putToken: function (token) {
            layui.data(setter.tableName, {
                key: 'token',
                value: token
            });
        },
        // 当前登录的用户ID
        getUserId: function () {
            var cacheData = layui.data(setter.tableName);
            if (cacheData) {
                return cacheData.login_user.userId;
            }
        },
        // 当前登录的用户
        getUser: function () {
            var cacheData = layui.data(setter.tableName);
            if (cacheData) {
                return cacheData.login_user;
            }
        },
        // 缓存user
        putUser: function (user) {
            layui.data(setter.tableName, {
                key: 'login_user',
                value: user
            });
        },
        // 缓存自定义参数
        putParameter: function (key,value) {
            layui.data(setter.tableName, {
                key: key,
                value: value
            });
        },
        // 获取自定义参数
        getParameter: function () {
            var cacheData = layui.data(setter.tableName);
            if (cacheData) {
                return cacheData;
            }
        },
        // 移除自定义参数
        removeParameter: function (key) {
            layui.data(setter.tableName, {
                key: key,
                remove: true
            });
        },
        // 获取用户所有权限
        getUserAuths: function () {
            var auths = [];
            var permissions = setter.getUser().permissionList;
            if (permissions) {
                for (var i = 0; i < permissions.length; i++) {
                    auths.push(permissions[i]);
                }
            }
            return auths;
        },

        // 往请求头设置token
        getBearerToken: function () {
            var cacheData = layui.data(setter.tableName);
            if (cacheData) {
                return 'Bearer ' + cacheData.token;
            }
        },

        // 往请求头设置token
        setTokenInHeader: function (headers) {
            var headers = headers;
            var token = setter.getToken();
            if (token && headers) {
                headers.Authorization = 'Bearer ' + token;
            }
            return headers;
        },
        // ajax请求结束后的处理，返回false阻止代码执行
        ajaxSuccessBefore: function (res, url, obj) {
            if (res.code === 401) {
                setter.removeToken();
                layer.msg('登录过期', {icon: 2, anim: 6, time: 1500}, function () {
                    location.reload();
                });
                return false;
            }
            return true;
        },
        // 路由不存在处理
        routerNotFound: function (r) {
            layer.alert('路由' + location.hash + '不存在', {
                title: '提示',
                skin: 'layui-layer-admin',
                btn: [],
                offset: '30px',
                anim: 6,
                shadeClose: true
            });
        }
    };

    // 更新组件缓存
    layui.config({
        version: setter.version
    });

    setter.base_server = setter.baseServer;  // 兼容旧版
    exports('setter', setter);
});
